package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.entity.CommonEntity;
import com.ruoyi.common.core.page.PageQuery;
import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.core.page.ResultEntity;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.GantryEntity;
import com.ruoyi.system.domain.GantryRecordEntity;
import com.ruoyi.system.domain.vo.GantryNameOptionVO;
import com.ruoyi.system.service.GantryRecordService;
import com.ruoyi.system.service.GantryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "物料管理")
@RestController
@RequestMapping("/gantry")
public class GantryController {
    @Autowired
    private GantryService gantryService;



    @Autowired
    private GantryRecordService gantryRecordService;



    @PostMapping("/font/gantry/list")
    @ApiOperation(value = "首页获取桁吊")
    public ResultEntity fontList(){
        var requestParam = new LambdaQueryWrapper<GantryEntity>();
        requestParam.eq(GantryEntity::getDelFlag,0);
        return ResultEntity.createSuccess(gantryService.list(requestParam));
    }

    @GetMapping("/font/gantry/list")
    @ApiOperation(value = "首页开箱记录")
    public ResultEntity fontRecordList(@RequestParam String gantryName){

        var nowTimeFormat =  LocalDateTime.now();

        LocalDateTime halfMonthAgo = nowTimeFormat.minus(30, ChronoUnit.DAYS);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 格式化日期时间
        String formattedDateTime = halfMonthAgo.format(formatter);

        var requestParam = new LambdaQueryWrapper<GantryRecordEntity>()
                .eq(GantryRecordEntity::getDelFlag,0).ge(GantryRecordEntity::getCreateTime, formattedDateTime)
                .orderByDesc(CommonEntity::getUpdateTime);

        if (!gantryName.isEmpty()) {
            requestParam.eq(GantryRecordEntity::getName, gantryName);
        }

        return ResultEntity.createSuccess(gantryRecordService.list(requestParam));
    }



    @PostMapping("/add")
    @ApiOperation(value = "添加桁吊")
    public ResultEntity add(@RequestBody GantryEntity params){

        params.setCreateBy(SecurityUtils.getLoginUser().getUser().getId());
        params.setUpdateBy(SecurityUtils.getLoginUser().getUser().getId());
        gantryService.saveOrUpdate(params);
        return ResultEntity.createSuccess();
    }


    @PostMapping("/open")
    @ApiOperation(value = "添加桁吊开箱记录")
    public ResultEntity open(@RequestBody GantryRecordEntity params){

        gantryRecordService.saveOrUpdate(params);
        return ResultEntity.createSuccess();
    }


    @GetMapping("/option/name")
    @ApiOperation(value = "桁吊名称下拉框")
    public ResultEntity name(){
        List<GantryNameOptionVO> result = gantryService.optionName();
        return ResultEntity.createSuccess(result.stream().map(GantryNameOptionVO::getName).collect(Collectors.toList()));
    }





    @GetMapping("/record_list")
    @ApiOperation(value = "获取桁吊")
    public ResultEntity recordList(PageQuery pageQuery){
        PageUtil.startPage(pageQuery);
        return ResultEntity.createSuccess(PageUtil.getPageList(gantryRecordService.list(new LambdaQueryWrapper<GantryRecordEntity>()
                .eq(GantryRecordEntity::getDelFlag,0)
                .orderByDesc(CommonEntity::getUpdateTime))));
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取桁吊")
    public ResultEntity list(PageQuery pageQuery){
        PageUtil.startPage(pageQuery);
        return ResultEntity.createSuccess(PageUtil.getPageList(gantryService.list(new LambdaQueryWrapper<GantryEntity>()
                .eq(GantryEntity::getDelFlag,0)
                .orderByDesc(CommonEntity::getUpdateTime))));
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除桁吊")
    public ResultEntity delete(@RequestParam("id") Long id){
        GantryEntity entity = new GantryEntity();
        entity.setId(id);
        entity.setDelFlag(1);
        gantryService.updateById(entity);
        return ResultEntity.createSuccess();
    }


    @GetMapping("/record/delete")
    @ApiOperation(value = "删除桁吊开箱记录")
    public ResultEntity deleteRecord(@RequestParam("id") Long id){
        GantryRecordEntity entity = new GantryRecordEntity();
        entity.setId(id);
        entity.setDelFlag(1);
        gantryRecordService.updateById(entity);
        return ResultEntity.createSuccess();
    }

}
